package com.atguigu.tingshu.album.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.TsCache;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	/**
	 * 获取所有一级分类数据
	 * @param o
	 * @return
	 */
	@Override
	public List<BaseCategory1> findAllCategory1() {
		return baseCategory1Mapper.selectList(null);
	}

	/**
	 * 根据一级分类ID，获取前7条置顶三级分类数据
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		//1、根据一级分类ID，获取二级分类IDlist
		List<BaseCategory2> category2List = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));
		List<Long> category2IdList = category2List.stream().map(baseCategory2 -> baseCategory2.getId()).collect(Collectors.toList());

		//2、获取三级分类数据
		//select * from base_category3 where category2_id in (101,102,103) and is_top=1 and is_deleted=0 limit 7
		return baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().in(BaseCategory3::getCategory2Id, category2IdList).eq(BaseCategory3::getIsTop, 1).last("limit 7"));

	}

	/**
	 * 根据一级分类Id，查询分类信息
	 * @param category1Id
	 * @return
	 */
	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {
		JSONObject categoryRes=new JSONObject();
		//1、根据一级分类ID，查询分类信息
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
		String category1Name = baseCategoryViewList.get(0).getCategory1Name();
		categoryRes.put("categoryId",category1Id);
		categoryRes.put("categoryName",category1Name);
		//2、根据二级分类ID，分组，查询分类信息
		Map<Long, List<BaseCategoryView>> category2Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = category2Map.entrySet().iterator();

		//	创建集合列表;
		ArrayList<JSONObject> categoryChild2List = new ArrayList<>();
		while (iterator.hasNext()) {
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			Long category2Id = entry.getKey();
			List<BaseCategoryView> categoryViewList = entry.getValue();
			//	创建一个一级分类对象
			JSONObject category2 = new JSONObject();
			category2.put("categoryId", category2Id);
			category2.put("categoryName", categoryViewList.get(0).getCategory2Name());
			//	获取三级分类数据
			List<JSONObject> list = categoryViewList.stream().map(categoryView -> {
				JSONObject category3 = new JSONObject();
				category3.put("categoryId", categoryView.getCategory3Id());
				category3.put("categoryName", categoryView.getCategory3Name());
				return category3;
			}).collect(Collectors.toList());
			//	将三级分类对象数据添加到二级分类集合中。
			category2.put("categoryChild", list);
			categoryChild2List.add(category2);
		}
		categoryRes.put("categoryChild", categoryChild2List);
		return categoryRes;
	}

	/**
	 * 根据三级分类ID，查一级分类ID和二级分类ID
	 * @param category3Id
	 * @return
	 */
	@Override
	@TsCache(prefix = "category:")
	public BaseCategoryView getCategoryView(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	/**
	 * 获取商品分类列表，包括一级分类、二级分类和三级分类。
	 * 分类数据从数据库中获取，并按层级结构组织成JSON对象列表返回。
	 *
	 * 一级分类包含其ID和名称，每个一级分类下可能有多个二级分类，
	 * 每个二级分类同样包含其ID和名称，并且每个二级分类下可能有多个三级分类，
	 * 三级分类也包含其ID和名称。
	 *
	 * @return List<JSONObject> 返回一个包含所有一级分类及其子分类信息的JSON对象列表。
	 *         结构如下：
	 *         [
	 *           {
	 *             "categoryId": 1,
	 *             "categoryName": "一级分类名称",
	 *             "categoryChild": [
	 *               {
	 *                 "categoryId": 2,
	 *                 "categoryName": "二级分类名称",
	 *                 "categoryChild": [
	 *                   {
	 *                     "categoryId": 3,
	 *                     "categoryName": "三级分类名称"
	 *                   },
	 *                   ...
	 *                 ]
	 *               },
	 *               ...
	 *             ]
	 *           },
	 *           ...
	 *         ]
	 */
	@Override
	public List<JSONObject> getBaseCategoryList() {
		//获取全部分类，
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		//设置一个Map,key:categoryId, value:List<List<BaseCategoryView>>
		Map<Long, List<BaseCategoryView>> categoryMap = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//循环遍历集合
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = categoryMap.entrySet().iterator();
		//设置结果集
		ArrayList<JSONObject> category1List = new ArrayList<>();
		while (iterator.hasNext()){
			//获取map中元素
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			//获取一级分类ID，和一级分类Name
			Long category1Id = entry.getKey();
			String category1Name = entry.getValue().get(0).getCategory1Name();
			//构建数据
			JSONObject category1 = new JSONObject();
			category1.put("categoryId",category1Id);
			category1.put("categoryName",category1Name);

			//基于category2Id分组，获取二级分类Map,key:category2Id，value:List<BaseCategoryView>
			Map<Long, List<BaseCategoryView>> category2Map = entry.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = category2Map.entrySet().iterator();

			ArrayList<JSONObject> category2Child = new ArrayList<>();
			while (iterator1.hasNext()) {
				//获取二级分类对象数据
				Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
				//获取二级分类ID，和二级分类Name
				Long category2Id = entry1.getKey();
				String category2Name = entry1.getValue().get(0).getCategory2Name();
				//构建二级分类JSONObject
				JSONObject category2 = new JSONObject();
				//获取二级分类ID，Name
				category2.put("categoryId",category2Id);
				category2.put("categoryName",category2Name);
				//获取三级分类List，
				List<JSONObject> category3Child = entry1.getValue().stream().map(baseCategoryView -> {
					JSONObject category3 = new JSONObject();
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					return category3;
				}).collect(Collectors.toList());
				//将三级分类结果，放入二级分类数据中，
				category2.put("categoryChild",category3Child);
				//将二级分类数据category2放到二级分类List中
				category2Child.add(category2);
			}
			category1.put("categoryChild",category2Child);
			//将一级分类数据加入结果集
			category1List.add(category1);
		}
		//返回数据
		return category1List;
	}

	/**
	 * 根据一级分类ID获取该分类下的属性和属性值数据
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		return baseAttributeMapper.selectBaseAttributeList(category1Id);
	}
}
















